package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShopCarMapper;
import com.sky.service.ShopCarService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShopCarServicelmpl implements ShopCarService {

    @Autowired
    ShopCarMapper shopCarMapper;
    @Autowired
    DishMapper dishMapper;
    @Autowired
    SetmealMapper setmealMapper;

    /**
     * 添加购物车
     * @param shoppingCartDTO
     */
    public void addShopCar(ShoppingCartDTO shoppingCartDTO) {
        log.info("获取购物车：{}",shoppingCartDTO);
        //new一个ShoppingCart对象 给其赋值
        ShoppingCart shoppingCart=new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //获取当前商品id
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);

        //判断当前的商品是否被加载上购物车
        List<ShoppingCart> list=shopCarMapper.list(shoppingCart);
        if (list!=null&&list.size()==1){
            //如果购物车存在商品，则只需要在商品数量上增加
            shoppingCart=list.get(0);
            shoppingCart.setNumber(shoppingCart.getNumber()+1);
            //新增购物车商品数量方法
            shopCarMapper.updateNumberById(shoppingCart);
        }else {
            //如果购物车没有商品数据，需要插入数据,获取菜品id
            Long dishId = shoppingCartDTO.getDishId();
            //判断购物车添加的是菜品还是套餐
            if (dishId!=null){
                Dish dish=dishMapper.getByids(dishId);
                //添加的是菜品
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            }else {
                //添加的是套餐,根据主键id获取套餐id
                Setmeal setmeal=setmealMapper.getid(shoppingCartDTO.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            //插入购物车里的新增商品信息
            shopCarMapper.insert(shoppingCart);


        }


    }

    /**
     * 查看购物车
     * @return
     */
    @Override
    public List<ShoppingCart> lookShopCart() {
        /*ShoppingCart build = ShoppingCart.builder().userId(BaseContext.getCurrentId()).build();
        List<ShoppingCart> ass= shopCarMapper.list(build);
        return ass;*/
        return shopCarMapper.list(ShoppingCart.builder().userId(BaseContext.getCurrentId()).build());
    }


    /**
     * 清空购物车
     */
    @Override
    public void cleanshopcart(){
        shopCarMapper.deleteByUserId(BaseContext.getCurrentId());
    }
}
